package com.example.common.exception;

import com.example.common.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request,Exception e){
        log.error("----------->捕捉到全局异常",e);
        if (e instanceof HwException){
            //..
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception",e);
        mv.addObject("message",e.getMessage());
        mv.addObject("url",request.getRequestURI());
        mv.setViewName("error");
        return mv;
    }
    @ExceptionHandler(value = HwException.class)
    public Result jsonErrorHandler(HttpServletRequest request,HwException e){
        return Result.failed(e.getMessage(),"error data");
    }
}
